//
//  LoginViewController.swift
//  QQ空间
//
//  Created by 叶竞泽 on 2017/5/7.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import UIKit
import MBProgressHUD

class LoginViewController: BaseViewController {

    // MARK: - 属性
    
    /// 记录是否保存密码
    var isRememberPassword: Bool {
        return rememberPasswordButton.isSelected
    }
    
    /// 记录是否自动登录
    var isAutoLogin: Bool {
        return autoLoginButton.isSelected
    }
    
    /// 帐号
    @IBOutlet weak var account: UITextField!
    /// 密码
    @IBOutlet weak var password: UITextField!
    /// 记住密码
    @IBOutlet weak var rememberPasswordButton: UIButton!
    /// 自动登录
    @IBOutlet weak var autoLoginButton: UIButton!
    /// 用户信息视图
    @IBOutlet weak var infoView: UIView!
    
    // MARK: - 方法
    
    /// 登录
    @IBAction func login() {
        // 获取帐号密码
        let account = self.account.text
        let password = self.password.text
        // 判断账号密码是否为空
        if !(account?.isEmpty)! && !(password?.isEmpty)! {
            // 判断帐号密码是否正确
            if account == "123" && password == "123" {
                // 提示登录成功
                showHudAndHideHud(title: "登录成功")
                let homeVC = HomeViewController()
                // 进入主界面
                view.window?.rootViewController = homeVC
            } else {
                // 提示密码错误
                shakeView(view: infoView)
            }
        } else {
            // 提示用户帐号密码不能为空
            showHudAndHideHud(title: "用户名和密码不能为空")
        }
    }
    
    /// 记住密码事件
    @IBAction func rememberPassword() {
        rememberPasswordButton.isSelected = !rememberPasswordButton.isSelected
    }
    
    /// 自动登录事件
    @IBAction func autoLogin() {
        if !autoLoginButton.isSelected {
            rememberPasswordButton.isSelected = true
            autoLoginButton.isSelected = true
        } else {
            rememberPasswordButton.isSelected = false
            autoLoginButton.isSelected = false
        }
    }
    
    /// 显示和隐藏hud
    private func showHudAndHideHud(title: String) {
        let hud = MBProgressHUD(view: view)
        hud.removeFromSuperViewOnHide = true
        view.addSubview(hud)
        hud.mode = .text
        hud.show(animated: true)
        hud.label.text = title
        hud.hide(animated: true, afterDelay: 1)
    }
    
    /// 抖动视图
    private func shakeView(view: UIView) {
        
        let shakeAnimation = CAKeyframeAnimation(keyPath: "transform.translation.x")
        shakeAnimation.values = [-10, 0, 10]
        shakeAnimation.duration = 0.08
        shakeAnimation.repeatCount = 3
        view.layer.add(shakeAnimation, forKey: nil)
    }
    
    // MARK: - 生命周期
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }
}

// MARK: - 设置
extension LoginViewController {
    fileprivate func setup() {
        account.delegate = self
        password.delegate = self
    }
}

// MARK: - 实现文本框代理
extension LoginViewController: UITextFieldDelegate {
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        switch textField.returnKeyType {
        case .next:
            password.becomeFirstResponder()
        case .go:
            login()
            view.endEditing(true)
        default:
            break
        }
        return true
    }
}
